package org.jeecg.modules.demo.com.kunpeng.service.impl;

import cn.hutool.core.text.StrBuilder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import org.jeecg.modules.demo.com.kunpeng.entity.*;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.ProjectCostApplyQueryDTO;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.UpdateOaProjectCostApplyDTO;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceCode;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceException;
import org.jeecg.modules.demo.com.kunpeng.mapper.*;
import org.jeecg.modules.demo.com.kunpeng.service.IProjectCostApplyService;
import org.jeecg.modules.demo.com.kunpeng.vo.OaProjectCostApplyPage;
import org.jeecg.modules.demo.com.kunpeng.vo.ProjectCostApplyListQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 项目费用申请表
 * @Author: jeecg-boot
 * @Date:   2022-12-15
 * @Version: V1.0
 */
@Service
@Slf4j
public class ProjectCostApplyServiceImpl extends ServiceImpl<ProjectCostApplyMapper, ProjectCostApply> implements IProjectCostApplyService {

    @Autowired
    private ProjectCostApplyMapper projectCostApplyMapper;
    @Autowired
    private OaApproveMapper oaApproveMapper;
    @Autowired
    private OaFlowStepMapper oaFlowStepMapper;
    @Autowired
    private OaMessageMapper oaMessageMapper;
    @Autowired
    private OaFlowRecordMapper oaFlowRecordMapper;
    @Autowired
    private OaAdminMapper oaAdminMapper;
    @Autowired
    private OaProjectMapper oaProjectMapper;
    @Autowired
    private OaAdminLogMapper oaAdminLogMapper;

    /**
     * 全局模糊查询
     * @param page
     * @param projectCostApplyQueryDTO
     * @return
     */
    @Override
    public Page<ProjectCostApplyListQueryVO> listByKeyWords(Page<ProjectCostApplyListQueryVO> page, ProjectCostApplyQueryDTO projectCostApplyQueryDTO) {
        log.info("开始进行全局模糊查询：service");
        System.out.println("projectCostApplyQueryDTO:"+projectCostApplyQueryDTO.toString());
        Page<ProjectCostApplyListQueryVO> list = page.setRecords(projectCostApplyMapper.listByKeyWords(page, projectCostApplyQueryDTO.getProjectName(), projectCostApplyQueryDTO.getState(), projectCostApplyQueryDTO.getKeyWords()));
        if(list.getRecords().size()>0){
            for(ProjectCostApplyListQueryVO projectCostApplyListQueryVO:list.getRecords()){
                OaApprove oaApprove = oaApproveMapper.selectOaApproveByPcaId(projectCostApplyListQueryVO.getId());
                if(null!=oaApprove){
                    projectCostApplyListQueryVO.setState(oaApprove.getCheckStatus().toString());
                    projectCostApplyMapper.updateProjectCostApplyStatus(projectCostApplyListQueryVO.getId(),oaApprove.getCheckStatus().toString());
                }
            }
        }
        return list;
    }

    /**
     * 项目列表页面跳转
     * @param page
     * @param projectId
     * @return
     */
    @Override
    public Page<ProjectCostApplyListQueryVO> listByProjectId(Page<ProjectCostApplyListQueryVO> page, String projectId) {
        Page<ProjectCostApplyListQueryVO> projectCostApplyListQueryVOPage = page.setRecords(projectCostApplyMapper.listByPjId(page, projectId));
        return projectCostApplyListQueryVOPage;
    }

    /**
     * 项目费用申请审批
     * @param oaProjectCostApplyPage
     * @return
     */
    @Override
    @Transactional
    public void insertOaProjectCostApply(OaProjectCostApplyPage oaProjectCostApplyPage) {
        ProjectCostApply projectCostApply=new ProjectCostApply();
        BeanUtils.copyProperties(oaProjectCostApplyPage,projectCostApply);

        //根据项目名称获取项目编号
        OaProject oaProject = oaProjectMapper.selectOaProjectByName(oaProjectCostApplyPage.getProjectName());
        projectCostApply.setProjectId(oaProject.getProjectId());
        projectCostApply.setState("0");
        projectCostApply.setCostMoney(oaProjectCostApplyPage.getCostMoney());
        System.out.println("抄送人："+oaProjectCostApplyPage.getCcPerson());
        System.out.println("审核人："+oaProjectCostApplyPage.getAuditor());
        System.out.println("创建人："+oaProjectCostApplyPage.getAdminId());
        //审核人信息
        OaAdmin checkPerson = oaAdminMapper.selectOaAdminDetail(oaProjectCostApplyPage.getAuditor());
        //创建人信息
        OaAdmin adminPerson = oaAdminMapper.selectOaAdminDetailById(oaProjectCostApplyPage.getAdminId());
        projectCostApply.setApplyPerson(adminPerson.getName());

        //审批表数据赋值
        OaApprove oaApprove=new OaApprove();
        //可能会有多个抄送人，抄送人字符串处理
        String copyPerson = oaProjectCostApplyPage.getCcPerson();
        String[] copyPersonList = copyPerson.split(",");
        if(copyPersonList.length>1){
            String copyPersonString=null;
            for (int i=0;i<copyPersonList.length;i++){
                OaAdmin ccPerson = oaAdminMapper.selectOaAdminDetail(copyPersonList[i]);
                System.out.println("获取的第"+i+"个抄送人信息："+ccPerson.toString());
                if(i>0){
                    copyPersonString=copyPersonString+","+ccPerson.getId();
                }else {
                    copyPersonString=ccPerson.getId().toString();
                }
            }
            System.out.println("多个抄送人Id拼接："+copyPersonString);
            oaApprove.setCopyUids(copyPersonString);
        }else {
            OaAdmin ccPerson = oaAdminMapper.selectOaAdminDetail(copyPerson);
            oaApprove.setCopyUids(ccPerson.getId().toString());
        }
        //项目费用申请补充审核人信息
        projectCostApply.setCopyName(copyPerson);
        projectCostApply.setAdminName(checkPerson.getName());
        projectCostApplyMapper.insert(projectCostApply);

        //补充项目费用申请审核数据
        oaApprove.setType(26);
        oaApprove.setFlowId(26);
        oaApprove.setContent(oaProjectCostApplyPage.getContent());
        oaApprove.setAdminId(adminPerson.getId());
        oaApprove.setDepartmentId(adminPerson.getDid());
        oaApprove.setProjectId(oaProjectCostApplyPage.getProjectId());
        oaApprove.setCheckStepSort(0);
        oaApprove.setCheckAdminIds(checkPerson.getId().toString());
        oaApprove.setCheckStatus(0);
        oaApprove.setDetailType(0);
        oaApprove.setProjectName(oaProjectCostApplyPage.getProjectName());
        oaApprove.setProjectPhase(oaProjectCostApplyPage.getProjectPhase());
        oaApprove.setAmount(oaProjectCostApplyPage.getCostMoney());
        Long createTime = System.currentTimeMillis();
        createTime/=1000L;
        oaApprove.setCreateTime(createTime.intValue());
        List<OaApprove> oaApproves = oaApproveMapper.listOaProjectApprove();
        OaApprove oa = oaApproves.get(oaApproves.size()-1);
        int num=oa.getId();
        oaApprove.setId(++num);
        oaApprove.setPcaId(projectCostApply.getId());
        oaApproveMapper.insert(oaApprove);

        //审批记录表数据赋值
        OaFlowRecord oaFlowRecord=new OaFlowRecord();
        oaFlowRecord.setActionId(num);
        oaFlowRecord.setStepId(0);
        oaFlowRecord.setCheckUserId(checkPerson.getId());
        oaFlowRecord.setStatus(0);
        oaFlowRecord.setType(1);
        oaFlowRecord.setCheckTime(createTime.intValue());
        oaFlowRecord.setPcaId(projectCostApply.getId());
        oaFlowRecordMapper.insert(oaFlowRecord);

        //审批步骤表数据赋值
        OaFlowStep oaFlowStep=new OaFlowStep();
        oaFlowStep.setActionId(num);
        oaFlowStep.setFlowType(0);
        oaFlowStep.setFlowUids(checkPerson.getId());
        oaFlowStep.setSort(0);
        oaFlowStep.setType(1);
        oaFlowStep.setCreateTime(createTime.intValue());
        oaFlowStep.setPcaId(projectCostApply.getId());
        oaFlowStepMapper.insert(oaFlowStep);

        //消息表数据赋值
        OaMessage oaMessage=new OaMessage();
        oaMessage.setActionId(num);
        oaMessage.setTitle("您收到一个【项目费用申请审批】，请及时审批");
        oaMessage.setContent("您有一个新的【项目费用申请审批】需要处理。");
        oaMessage.setTemplate(55);
        oaMessage.setFromUid(adminPerson.getId());
        oaMessage.setToUid(checkPerson.getId());
        oaMessage.setStatus(1);
        oaMessage.setIsDraft(1);
        oaMessage.setModuleName("project");
        oaMessage.setControllerName("approve");
        oaMessage.setActionName("add");
        oaMessage.setSendTime(createTime.intValue());
        oaMessage.setCreateTime(createTime.intValue());
        oaMessage.setPcaId(projectCostApply.getId());
        oaMessageMapper.insert(oaMessage);
    }

    /**
     * 根据id修改项目费用申请状态
     * @param id
     * @param state
     */
    @Override
    public void updateProjectCostApplyStatus(String id, String state) {
        int num = projectCostApplyMapper.updateProjectCostApplyStatus(id, state);
        if(num!=1){
            String message="修改失败，请联系管理员！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    /**
     * 根据id修改项目费用申请删除状态
     * @param id
     */
    @Override
    public void updateProjectCostApplyIsDeleteById(String id) {
        int num = projectCostApplyMapper.updateProjectCostApplyIsDeleteById(id);
        if(num!=1){
            String message="删除失败，请联系管理员！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    /**
     * 根据id修改项目费用申请关联数据
     */
    @Override
    @Transactional
    public void updateProjectCostApplyById(UpdateOaProjectCostApplyDTO updateOaProjectCostApplyDTO) {
        //删除项目费用申请关联数据
        oaApproveMapper.deleteOaApproveByPcaId(updateOaProjectCostApplyDTO.getId());
        oaFlowRecordMapper.deleteOaFlowRecordByPcaId(updateOaProjectCostApplyDTO.getId());
        oaFlowStepMapper.deleteOaFlowStepByPcaId(updateOaProjectCostApplyDTO.getId());
        oaMessageMapper.deleteOaMessageByPcaId(updateOaProjectCostApplyDTO.getId());

        System.out.println("编辑项目费用申请DTO数据："+updateOaProjectCostApplyDTO.toString());

        //重新插入
        ProjectCostApply projectCostApply=new ProjectCostApply();
        BeanUtils.copyProperties(updateOaProjectCostApplyDTO,projectCostApply);
        System.out.println("编辑项目费用申请复制完数据："+projectCostApply);

        //根据项目名称获取项目编号
        OaProject oaProject = oaProjectMapper.selectOaProjectByName(updateOaProjectCostApplyDTO.getProjectName());
        projectCostApply.setProjectId(oaProject.getProjectId());
        projectCostApply.setCostMoney(updateOaProjectCostApplyDTO.getCostMoney());
        System.out.println("抄送人："+updateOaProjectCostApplyDTO.getCcPerson());
        System.out.println("审核人："+updateOaProjectCostApplyDTO.getAuditor());
        System.out.println("创建人："+updateOaProjectCostApplyDTO.getAdminId());

        //审核人信息
        OaAdmin checkPerson = oaAdminMapper.selectOaAdminDetail(updateOaProjectCostApplyDTO.getAuditor());
        //创建人信息
        OaAdmin adminPerson = oaAdminMapper.selectOaAdminDetailById(updateOaProjectCostApplyDTO.getAdminId());
        //时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        //获取日志表数据信息
        /*List<OaAdminLog> oaAdminLogs = oaAdminLogMapper.oaAdminLogList();
        OaAdminLog oAdminLog = oaAdminLogs.get(oaAdminLogs.size() - 1);*/
        Integer oaAdminLogId=oaAdminLogMapper.selectOaAdminLogMaxId()+1;
        //生成项目费用申请修改日志
        OaAdminLog oaAdminLog=new OaAdminLog();
        oaAdminLog.setId(oaAdminLogId);
        oaAdminLog.setUid(adminPerson.getId());
        oaAdminLog.setName(adminPerson.getName());
        oaAdminLog.setType("edit");
        oaAdminLog.setAction("编辑");
        oaAdminLog.setSubject("项目费用申请");
        oaAdminLog.setTitle("新建/编辑");
        oaAdminLog.setContent(adminPerson.getName()+"在"+time+"编辑了项目费用申请");
        oaAdminLog.setModule("project");
        oaAdminLog.setController("rule");
        oaAdminLog.setRuleMenu("");
        oaAdminLog.setIp("");
        oaAdminLog.setParamId(1);
        oaAdminLog.setParam("");
        oaAdminLog.setStatus(1);
        //数据创建时间
        Long createTime = System.currentTimeMillis();
        createTime/=1000L;
        oaAdminLog.setCreateTime(createTime.intValue());
        oaAdminLogMapper.insert(oaAdminLog);
        //审批表数据赋值
        OaApprove oaApprove=new OaApprove();
        //可能会有多个抄送人
        String copyPerson = updateOaProjectCostApplyDTO.getCcPerson();
        String[] copyPersonList = copyPerson.split(",");
        if(copyPersonList.length>1){
            String copyPersonString=null;
            for (int i=0;i<copyPersonList.length;i++){
                OaAdmin ccPerson = oaAdminMapper.selectOaAdminDetail(copyPersonList[i]);
                if(i>0){
                    copyPersonString=copyPersonString+","+ccPerson.getId();
                }else {
                    copyPersonString=ccPerson.getId().toString();
                }
            }
            oaApprove.setCopyUids(copyPersonString);
        }else {
            OaAdmin ccPerson = oaAdminMapper.selectOaAdminDetail(copyPerson);
            oaApprove.setCopyUids(ccPerson.getId().toString());
        }

        //补充项目费用申请数据
        projectCostApply.setCopyName(copyPerson);
        projectCostApply.setAdminName(checkPerson.getName());
        projectCostApply.setApplyPerson(adminPerson.getName());
        projectCostApplyMapper.updateProjectCostApplyById(projectCostApply);

        //补充项目费用申请审批数据
        oaApprove.setType(26);
        oaApprove.setFlowId(26);
        oaApprove.setContent(updateOaProjectCostApplyDTO.getContent());
        oaApprove.setAdminId(adminPerson.getId());
        oaApprove.setDepartmentId(adminPerson.getDid());
        oaApprove.setProjectId(updateOaProjectCostApplyDTO.getProjectId());
        oaApprove.setCheckStepSort(0);
        oaApprove.setCheckAdminIds(checkPerson.getId().toString());
        oaApprove.setCheckStatus(0);
        oaApprove.setDetailType(0);
        oaApprove.setProjectName(updateOaProjectCostApplyDTO.getProjectName());
        oaApprove.setProjectPhase(updateOaProjectCostApplyDTO.getProjectPhase());
        oaApprove.setAmount(updateOaProjectCostApplyDTO.getCostMoney());
        oaApprove.setCreateTime(createTime.intValue());
        List<OaApprove> oaApproves = oaApproveMapper.listOaProjectApprove();
        OaApprove oa = oaApproves.get(oaApproves.size()-1);
        int num=oa.getId();
        oaApprove.setId(++num);
        oaApprove.setPcaId(projectCostApply.getId());
        oaApproveMapper.insert(oaApprove);

        //审批记录表数据赋值
        OaFlowRecord oaFlowRecord=new OaFlowRecord();
        oaFlowRecord.setActionId(num);
        oaFlowRecord.setStepId(0);
        oaFlowRecord.setCheckUserId(checkPerson.getId());
        oaFlowRecord.setStatus(0);
        oaFlowRecord.setType(1);
        oaFlowRecord.setCheckTime(createTime.intValue());
        oaFlowRecord.setPcaId(projectCostApply.getId());
        oaFlowRecordMapper.insert(oaFlowRecord);

        //审批步骤表数据赋值
        OaFlowStep oaFlowStep=new OaFlowStep();
        oaFlowStep.setActionId(num);
        oaFlowStep.setFlowType(0);
        oaFlowStep.setFlowUids(checkPerson.getId());
        oaFlowStep.setSort(0);
        oaFlowStep.setType(1);
        oaFlowStep.setCreateTime(createTime.intValue());
        oaFlowStep.setPcaId(projectCostApply.getId());
        oaFlowStepMapper.insert(oaFlowStep);

        //消息表数据赋值
        OaMessage oaMessage=new OaMessage();
        oaMessage.setActionId(num);
        oaMessage.setTitle("您收到一个【项目费用申请审批】，请及时审批");
        oaMessage.setContent("您有一个新的【项目费用申请审批】需要处理。");
        oaMessage.setTemplate(55);
        oaMessage.setFromUid(adminPerson.getId());
        oaMessage.setToUid(checkPerson.getId());
        oaMessage.setStatus(1);
        oaMessage.setIsDraft(1);
        oaMessage.setModuleName("project");
        oaMessage.setControllerName("approve");
        oaMessage.setActionName("add");
        oaMessage.setSendTime(createTime.intValue());
        oaMessage.setCreateTime(createTime.intValue());
        oaMessage.setPcaId(projectCostApply.getId());
        oaMessageMapper.insert(oaMessage);
    }

}
